package com.ming.service.impl;

import com.ming.mapper.UserMapper;
import com.ming.pojo.User;
import com.ming.service.UserService;
import com.ming.utils.Md5Util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Slf4j
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public User findByUserName(String username) {
        User u = userMapper.findByUserName(username);
        return u;
    }

    @Override
    public void register(String username, String password) {
        //加密
        String md5String = Md5Util.getMD5String(password);
        LocalDateTime createTime = LocalDateTime.now();
        LocalDateTime updateTime = LocalDateTime.now();
        User user = new User();
        user.setUsername(username);
        user.setPassword(md5String);
        user.setCreateTime(createTime);
        user.setUpdateTime(updateTime);
        //添加
        userMapper.add(user);
    }

    @Override
    public void updatePwd(String newPwd, Integer id) {
        String md5String = Md5Util.getMD5String(newPwd);
        LocalDateTime updateTime = LocalDateTime.now();
        User user = new User();
        user.setId(id);
        user.setPassword(md5String);
        user.setUpdateTime(updateTime);
        userMapper.updatePwd(user);
    }
}
